package com.emmmya.ocs.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;


/**
 * @author Exrickx
 */
@Slf4j
public class JasyptUtil {

    /**
     * Jasypt生成加密结果
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待加密值
     * @return
     */
    public static String encyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.encrypt(value);
        return result;
    }

    /**
     * 解密
     * @param password 配置文件中设定的加密密码 jasypt.encryptor.password
     * @param value 待解密密文
     * @return
     */
    public static String decyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        encryptor.decrypt(value);
        String result = encryptor.decrypt(value);
        return result;
    }

    public static SimpleStringPBEConfig cryptor(String password){
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize(1);
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        return config;
    }

    public static void main(String[] args){
       /* String s = CommonUtil.renamePic("baiduwangpan.exe");
        System.out.println(s);
        boolean equals = "123".equals(null);
        System.out.println(equals);
        //测试data类的方法
        Date date = new Date();
        try {
            Thread thread = Thread.currentThread();
            boolean before = new Date().before(date);
            System.out.println(before);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        //加密 若修改了第一个参数加密password记得在配置文件同步修改
        //System.out.println(encyptPwd("harin","jdbc:mysql://45.15.131.138:3306/routine?characterEncoding=UTF-8&serverTimezone=UTC"));
//        System.out.println(encyptPwd("harin","jdbc:mysql://45.15.131.138:3306/routine?characterEncoding=UTF-8&serverTimezone=UTC"));
//        System.out.println(encyptPwd("harin","kele2021"));
//        System.out.println(encyptPwd("harin","jdbc:mysql://8.134.19.243:3306/chms?characterEncoding=UTF-8&serverTimezone=UTC"));
//        System.out.println(encyptPwd("harin","laijieguan"));
//        System.out.println(encyptPwd("harin","jieguan1994"));
        System.out.println(encyptPwd("ocs2022","jdbc:mysql://127.0.0.1:3306/ocs?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true&serverTimezone=GMT%2b8"));
        System.out.println(encyptPwd("ocs2022","MD5"));
        System.out.println(encyptPwd("ocs2022","c0a63feebc0dbe5cccf62deb39250ab4"));
        System.out.println(encyptPwd("ocs2022","100"));
        System.out.println(encyptPwd("ocs2022","sdjo//32*(&^))sdasd"));
        //宿舍的数据库密码:jieguan1994
        //jwt密钥：sdjo//32*(&^))sdasd
        //公司电脑数据库密码:swdtest
        //阿里云的mysql密码是:Jieguan1994
        //解密
        //System.out.println(decyptPwd("harin","e2mNrNR42PMRejqt13SCTOGFOboMTCCYNPnhuhlcHxhQDq3ifS7O1a+rcItHrEHa9eOkVGkLcWkg5wN+6zIXHkDty5VGzTTq9oRZ+3KeycdyQdBkOaxegOxPf7eW2QBgt7EvIhNGadD1s9FCwtXePA=="));
        //System.out.println(decyptPwd("ssc2022","BFoi1cesqIuuhD5wiQ6S4LCxO4h1wPYA83xesdzJM5t049lXcRAUxSxtoii7V/Ff+cr39HAcjapV/XCLZem7ryfAV1+ZHPmZIg9pRmZXRLY="));
        System.out.println(decyptPwd("ssc2022","xvQ5iD/ZSGXfqUV9RxzJ8X+AlyWaji4Vl/IFRXkBaQti2GLPEdXe56XW0RCa1XT9Euh2casolAz67g/lOhDARw=="));
    }
}
